package fauxman.FauxManStates;

import java.awt.Point;
import java.util.Collection;

import fauxman.DynamicElements.Bonus;
import fauxman.DynamicElements.FauxMan;
import fauxman.DynamicElements.Food;
import fauxman.DynamicElements.Ghost;
import fauxman.DynamicElements.Pellet;
import fauxman.Enums.GameElementName;
import fauxman.StaticElements.PlayableArea;
import fauxman.interfaces.DynamicElement;
import fauxman.model.WorldModel;
import fauxman.states.EatenGhostState;
import fauxman.states.GhostState;
import fauxman.states.VulnerableGhostState;
import fauxman.utilities.ActionUtil;

public class NormalFauxManState extends FauxManState {
	private static NormalFauxManState instance;
	
	private NormalFauxManState(){
	}
	
	public static NormalFauxManState getInstance(){
		if (instance==null)
			instance = new NormalFauxManState();
		return instance;
	}
	
	@Override
	public void setNextDirection(int[] action, WorldModel model, FauxMan fauxMan) {
		int[] nextDir = action;
		//Point conP = fauxMan.getConHitbox().x;
		int conHitboxX = fauxMan.getConHitbox().x;
		int conHitboxY = fauxMan.getConHitbox().y;
		Point disP = fauxMan.getDiscretePosition();
		if(model.isPlayableAreaIntersection(disP.x, disP.y))
		{
			fauxMan.setState(CorneringFauxManState.getInstance());
			FauxManCorneringAction cornerAct = convertToCorneringDirection(conHitboxX, conHitboxY, model, action);
			if(cornerAct != null && ActionUtil.simulateAction(fauxMan, model, cornerAct.getDirection()))
			{
				
				Point endP = ActionUtil.getNextPosition(new Point(conHitboxX, conHitboxY), cornerAct.getTrackLength()*ActionUtil.CONTINUOUS_UNITS, cornerAct.getDirection());
				fauxMan.setCorneringTrailEnd(endP);
				fauxMan.setCorneringEndDirection(action); //At end of cornering trail and on playable track turn direction to the requested action
				nextDir = cornerAct.getDirection();
			}
			else
			{
				fauxMan.setState(NormalFauxManState.getInstance());
			}
		}
		fauxMan.setNextDirection(nextDir);
	}

	@Override
	public Collection<? extends PlayableArea> getPlayableArea(WorldModel model) {
		return model.getPlayableArea();
	}
}

